package jp.sourceforge.akjrcp.audiotools.operations;

import java.util.ArrayList;
import java.util.List;

import jp.sourceforge.akjrcp.audiotools.WaveFormViewer;
import jp.sourceforge.akjrcp.subtitle.StandardSubObject;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.AbstractOperation;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public class SubtitleChangeOperation extends AbstractOperation {
	WaveFormViewer viewer;
	List<StandardSubObject> beforeSubtitles;
	List<StandardSubObject> afterSubtitles;
	
	public SubtitleChangeOperation(WaveFormViewer viewer,List<StandardSubObject> beforeSubtitles,List<StandardSubObject> afterSubtitles) {
		super("");
		this.viewer=viewer;
		this.beforeSubtitles=beforeSubtitles;
		this.afterSubtitles=afterSubtitles;
	}

	@Override
	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
			throws ExecutionException {
		//this type do nothing in execute,do something  before call execute.
		return Status.OK_STATUS;

	}

	@Override
	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
			throws ExecutionException {
		
		viewer.srtFigure.getStandardSubObjectList().clear();
		for (int i = 0; i < afterSubtitles.size(); i++) {
			viewer.srtFigure.getStandardSubObjectList().add(afterSubtitles.get(i));
		}
		
		viewer.lastSubtitle=null;
		viewer.waveInfoCanvas.unselect();
		viewer.srtFigure.updateSubtitles();
		viewer.selectionChangedNone();
		viewer.setSubtitleModified(true);
		
		return Status.OK_STATUS;

	}

	@Override
	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
			throws ExecutionException {
		
		viewer.srtFigure.getStandardSubObjectList().clear();
		for (int i = 0; i < beforeSubtitles.size(); i++) {
			viewer.srtFigure.getStandardSubObjectList().add(beforeSubtitles.get(i));
		}
		
		viewer.lastSubtitle=null;
		viewer.waveInfoCanvas.unselect();
		viewer.srtFigure.updateSubtitles();
		viewer.selectionChangedNone();
		viewer.setSubtitleModified(true);
		return Status.OK_STATUS;

	}

}
